﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheQuest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        WMPLib.WindowsMediaPlayer songPlayer;
        private Game game;
        private Random random = new Random();
        private void Form1_Load(object sender, EventArgs e)
        {
            game = new Game(new Rectangle(84, 60, 480, 172));
            songPlayer = new WMPLib.WindowsMediaPlayer();
            songPlayer.URL = @"ZapBeat.mp3";
            songPlayer.settings.setMode("loop", true);
            songPlayer.controls.play();
            game.NewLevel(random);
            UpdateCharacters();
        }

        private void UpdateCharacters()
        {
            player.Location = game.PlayerLocation;
            playerHitPointLabel.Text = game.PlayerHitPoint.ToString();
            bool showBat = false;
            bool showGhost = false;
            bool showGhoul = false;
            int enemyShown = 0;


            batHitPointLabel.Text = "0";
            ghostHitPointLabel.Text = "0";
            ghoulHitPointLabel.Text = "0";

            foreach (Enemy enemy in game.Enemies)
            {
                if (enemy is Bat)
                {
                    bat.Location = enemy.Location;
                    batHitPointLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showBat = true;
                        enemyShown++;
                    }
                    else
                    {
                        showBat = false;
                        batHitPointLabel.Text = "0";
                    }
                    if (showBat)
                        bat.Visible = true;
                    else
                        bat.Visible = false;
                }

                if (enemy is Ghost)
                {
                    ghost.Location = enemy.Location;
                    ghostHitPointLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showGhost = true;
                        enemyShown++;
                    }
                    else
                    {
                        showGhost = false;
                        ghostHitPointLabel.Text = "0";
                    }
                    if (showGhost)
                        ghost.Visible = true;
                    else
                        ghost.Visible = false;
                }
                if (enemy is Ghoul)
                {
                    ghoul.Location = enemy.Location;
                    ghoulHitPointLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showGhoul = true;
                        enemyShown++;
                    }
                    else
                    {
                        showGhoul = false;
                        ghoulHitPointLabel.Text = "0";
                    }
                    if (showGhoul)
                        ghoul.Visible = true;
                    else
                        ghoul.Visible = false;
                }
            }

            sword.Visible = false;
            bow.Visible = false;
            mace.Visible = false;
            redPotion.Visible = false;
            bluePotion.Visible = false;

            Control weaponControl = null;
            switch (game.WeaponInRoom.Name)
            {
                case "sword" :
                    weaponControl = sword;
                    break;
                case "bow" :
                    weaponControl = bow;
                    break;
                case "mace" :
                    weaponControl = mace;
                    break;
                case "bluePotion" :
                    weaponControl = bluePotion;
                    break;
                case "redPotion" :
                    weaponControl = redPotion;
                    break;
                default:
                    break;
            }
            weaponControl.Visible = true;

            if (game.CheckPlayerInventory("sword"))
                inventorySwordPictureBox.Visible = true;
            if (game.CheckPlayerInventory("bow"))
                inventoryBowPictureBox.Visible = true;
            if (game.CheckPlayerInventory("mace"))
                inventoryMacePictureBox.Visible = true;
            if (game.CheckPlayerInventory("bluePotion"))
                inventoryBluePotionPictureBox.Visible = true;
                else inventoryBluePotionPictureBox.Visible = false;
            if (game.CheckPlayerInventory("redPotion"))
                inventoryRedPotionPictureBox.Visible = true;
                else inventoryRedPotionPictureBox.Visible = false;

            weaponControl.Location = game.WeaponInRoom.Location;
            if (game.WeaponInRoom.PickedUp)
                weaponControl.Visible = false;
            else
                weaponControl.Visible = true;

            if (game.PlayerHitPoint <= 0)
            {
                MessageBox.Show("You died");
                Application.Exit();
            }

            if (enemyShown < 1)
            {
                MessageBox.Show("You have defeated the enemy on this level");
                game.NewLevel(random);
                UpdateCharacters();
            }
            
        }

        private void drawAttackButton()
        {
            upAttackButton.Text = "↑";
            leftAttackButton.Visible = true;
            rightAttackButton.Visible = true;
            downAttackButton.Visible = true;
        }
        private void drawPotionButton()
        {
            upAttackButton.Text = "Drink";
            leftAttackButton.Visible = false;
            rightAttackButton.Visible = false;
            downAttackButton.Visible = false;
        }

        private void inventorySwordPictureBox_Click(object sender, EventArgs e)
        {
            string weaponName = "sword";
            if (game.CheckPlayerInventory(weaponName))
            {
                game.Equip(weaponName);
                inventorySwordPictureBox.BorderStyle = BorderStyle.FixedSingle;
                inventoryBowPictureBox.BorderStyle = BorderStyle.None;
                inventoryMacePictureBox.BorderStyle = BorderStyle.None;
                inventoryBluePotionPictureBox.BorderStyle = BorderStyle.None;
                inventoryRedPotionPictureBox.BorderStyle = BorderStyle.None;
                drawAttackButton();
            }
        }

        private void inventoryBowPictureBox_Click(object sender, EventArgs e)
        {
            string weaponName = "bow";
            if (game.CheckPlayerInventory(weaponName))
            {
                game.Equip(weaponName);
                inventoryBowPictureBox.BorderStyle = BorderStyle.FixedSingle;
                inventorySwordPictureBox.BorderStyle = BorderStyle.None;
                inventoryMacePictureBox.BorderStyle = BorderStyle.None;
                inventoryBluePotionPictureBox.BorderStyle = BorderStyle.None;
                inventoryRedPotionPictureBox.BorderStyle = BorderStyle.None;
                drawAttackButton();
            }
        }

        private void inventoryMacePictureBox_Click(object sender, EventArgs e)
        {
            string weaponName = "mace";
            if (game.CheckPlayerInventory(weaponName))
            {
                game.Equip(weaponName);
                inventoryMacePictureBox.BorderStyle = BorderStyle.FixedSingle;
                inventoryBowPictureBox.BorderStyle = BorderStyle.None;
                inventorySwordPictureBox.BorderStyle = BorderStyle.None;
                inventoryBluePotionPictureBox.BorderStyle = BorderStyle.None;
                inventoryRedPotionPictureBox.BorderStyle = BorderStyle.None;
                drawAttackButton();
            }
        }

        private void inventoryBluePotionPictureBox_Click(object sender, EventArgs e)
        {
            string weaponName = "bluePotion";
            if (game.CheckPlayerInventory(weaponName))
            {
                game.Equip(weaponName);
                inventoryBluePotionPictureBox.BorderStyle = BorderStyle.FixedSingle;
                inventoryBowPictureBox.BorderStyle = BorderStyle.None;
                inventoryMacePictureBox.BorderStyle = BorderStyle.None;
                inventorySwordPictureBox.BorderStyle = BorderStyle.None;
                inventoryRedPotionPictureBox.BorderStyle = BorderStyle.None;
                drawPotionButton();
            }            
        }

        private void inventoryRedPotionPictureBox_Click(object sender, EventArgs e)
        {
            string weaponName = "redPotion";
            if (game.CheckPlayerInventory(weaponName))
            {
                game.Equip(weaponName);
                inventoryRedPotionPictureBox.BorderStyle = BorderStyle.FixedSingle;
                inventoryBowPictureBox.BorderStyle = BorderStyle.None;
                inventoryMacePictureBox.BorderStyle = BorderStyle.None;
                inventoryBluePotionPictureBox.BorderStyle = BorderStyle.None;
                inventorySwordPictureBox.BorderStyle = BorderStyle.None;
                drawPotionButton();
            }
        }
        private void upMoveButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Up, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }
        private void rightMoveButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Right, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void downMoveButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Down, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void leftMoveButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Left, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 'd' || e.KeyChar == 'D')
            {
                game.Move(Direction.Right, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'a' || e.KeyChar == 'A')
            {
                game.Move(Direction.Left, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'w' || e.KeyChar == 'W')
            {
                game.Move(Direction.Up, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 's' || e.KeyChar == 'S')
            {
                game.Move(Direction.Down, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'i' || e.KeyChar == 'I')
            {
                game.Attack(Direction.Up, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'j' || e.KeyChar == 'J')
            {
                game.Attack(Direction.Left, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'l' || e.KeyChar == 'L')
            {
                game.Attack(Direction.Right, random);
                UpdateCharacters();
            }
            if (e.KeyChar == 'k' || e.KeyChar == 'K')
            {
                game.Attack(Direction.Down, random);
                UpdateCharacters();
            }

        }

        private void upAttackButton_Click(object sender, EventArgs e)
        {
            if (upAttackButton.Text=="Drink")
            {
                upAttackButton.Text = "↑";
                leftAttackButton.Visible = true;
                rightAttackButton.Visible = true;
                downAttackButton.Visible = true;
                inventorySwordPictureBox.BorderStyle = BorderStyle.FixedSingle;
            }
            game.Attack(Direction.Up, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void rightAttackButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Right, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void downAttackButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Down, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }

        private void leftAttackButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Left, random);
            UpdateCharacters();
            this.ActiveControl = null;
        }
    }
}
